import xlrd,timeit,struct,io,math,random,sys

import openpyxl
from openpyxl.worksheet._read_only import ReadOnlyWorksheet
from collections import OrderedDict,Counter


fileName = "configExcels/n_npc.xlsx"

# st = timeit.default_timer()
# doc = xlrd.open_workbook(fileName)
# et = timeit.default_timer()
# print("xlrd=",et -st)

st = timeit.default_timer()
# wb = openpyxl.load_workbook(fileName,read_only=True,data_only=True)
# sheetNames = wb.sheetnames
# wb.worksheets
# npcFitSheet = wb['t_NpcFitConfig']
# print("npcFitSheet.title=",npcFitSheet.title,"npcFitSheet=",npcFitSheet.max_row)

# fields = []
# num = 0
# for row in npcFitSheet.rows:
#     num += 1
#     if num==2:
#         for cell in row:
#             print("cell.value=",cell)
            
totalBytes = bytes()    
# count = 1310877
count = 200000

#通过IO写入 速度快N倍
bytesIO = io.BytesIO()

buffer = io.BufferedWriter(bytesIO)

total = 0
for i in range(count):
    b = struct.pack("<i",i)
    # totalBytes += b
    buffer.write(b) ##通过IO写入 速度快N倍
    total += 4

buffer.flush()

# totalBytes = bytesIO.getbuffer().tobytes()

# bytesIO.seek(0)
# reader = io.BufferedReader(bytesIO)
# count = 10
# startIdx = 0
# endIdx = 4
# for item in range(count):
#     # b = reader.read(4)
#     b = totalBytes[startIdx:endIdx]
#     startIdx += 4
#     endIdx += 4
#     temp = struct.unpack("<i",b)
#     print("temp=",temp,reader.tell())

et = timeit.default_timer()
print("xxx =",et -st,"totalBytes=",total,"totalBytes=",len(totalBytes))

# print("openpyxl=",et -st,"sheetNames=",sheetNames,"npcFitSheet.rows=",num,"fields=",fields)

without_dup_list = [1,2,3,4,4,5,6,6,7,7,8,9,9,10]
od = OrderedDict.fromkeys(without_dup_list)
print(od.keys())

tempFields = ["id","name ","icon","desc"," name ","name"]

fields = [f.strip() for f in tempFields if f!=None and f.strip()!=""]
c = Counter(fields)
dupFields = [f for f in fields if c[f]>1]
dupSet = set(dupFields)

print("fields",fields)
print("fields","_".join(fields),"dupFields=",dupFields,"dupSet=",dupSet,",".join(dupSet))

realFields = ["id","name","icon","cat"]
dts = ["int","string","int","int"]
vals = [1,"",0,10]
bitNums = []
bitRets = []

flags = [1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0]
flags = [1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1]

num = len(flags)
mask = 1

tempArr = []

for i in range(num):
    bit = i + 1
    flag = flags[i] == 1#random.random() > 0.5
    temp = 0
    if flag:#有值
        mask = mask | 1<<bit #记为1
        temp = 1
    tempArr.append(temp)
    bitNums.append(mask)

print("mask=",mask)
tempArr2 = []
for i in range(num):
    bit = i + 1
    temp = 1<<bit
    ret = mask & temp #通过 and 操作得到该位是否是1
    t = 1 if ret>0 else 0 
    #print("bit=",bit,"temp=",temp,"ret=",ret)
    tempArr2.append(t)

st1 = ",".join([str(x) for x in tempArr])
st2 = ",".join([str(x) for x in tempArr2])
print("结果是否一致:",st1==st2,mask>sys.maxsize,sys.maxsize,pow(2,63)-1)
print("st1",st1,"st2=",st2)
